package tgc.edu.examxy.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.examxy.custom.AjaxResult;
import tgc.edu.examxy.custom.DataGridParam;
import tgc.edu.examxy.custom.DataGridUtils;
import tgc.edu.examxy.custom.dict.entity.SystemData;
import tgc.edu.examxy.custom.dict.service.SystemDataService;
import tgc.edu.examxy.entity.AskForleave;
import tgc.edu.examxy.entity.Student;
import tgc.edu.examxy.security.User2;
import tgc.edu.examxy.service.AskForleaveService;
import tgc.edu.examxy.service.StudentService;
import tgc.edu.examxy.service.SysFileService;
import tgc.edu.examxy.web.form.AskForleaveForm;
/****
 * 请假
 * @author 936642284
 *
 */
@Controller
@RequestMapping(value = "/askForleave")
public class AskForleaveController {
	
	@Autowired
	private StudentService studentservice;
	@Autowired
	private AskForleaveService service;
	@Autowired
	private SysFileService fileservice;
	@Autowired
	private SystemDataService sysservice;
	/***
	 * 跳转到请假类型
	 * @return
	 */
	@RequestMapping(value = "/manage")
	private String manage(ModelMap map) {
		Order order=new Order(Direction.fromString("ASC"), "createDate");
		Sort sort=Sort.by(order);
		List<SystemData> findBySystemDictionariesCode = sysservice.findBySystemDictionariesCode("askForleave", sort);
		map.put("model", findBySystemDictionariesCode);
		return "askForleave/manage";
	}
	
	@RequestMapping(value = "/goleave")
	@ResponseBody
	private Object goleave(AskForleaveForm form){
		SecurityContext ctx = SecurityContextHolder.getContext();
		Authentication auth = ctx.getAuthentication();
		User2 user = (User2) auth.getPrincipal();
		Student students = studentservice.findByUsername(user.getUsername());
		String time = form.getStartDate();
		String[] split = time.split(" - ");
		AskForleave ask = new AskForleave();
		ask.setStartDate(split[0]);
		ask.setEndDate(split[1]);
		ask.setStudentId(students.getId());
		ask.setType(form.getType());
		ask.setReasonforleave(form.getReasonforleave());
		ask.setState(1);
		ask.setLeavefile(form.getLeavefile());
		ask.setTbClass(students.getTbClass().getName());
		ask.setStuName(students.getName());
		service.save(ask);
		fileservice.DeleteAll(form.getLeavefile());
		return new AjaxResult(true, "申请已提交,前往  请假记录  可查看进度");
	}
	
	@RequestMapping(value= "/leave")
	private String leave() {
		return "askForleave/leave";
	}
	
	/****
	 * 显示自己的请假情况
	 * @return
	 */
	@RequestMapping(value="/page")
	@ResponseBody
	private Object toleavepage(DataGridParam param,AskForleaveForm form) {
		Sort sort=Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<AskForleave> spec = buildSpec(form);
		Page<AskForleave> page = service.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<AskForleave> buildSpec(AskForleaveForm form) {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Student usernames = studentservice.findByUsername(user.getUsername());
	    
	    Specification<AskForleave> spe = new Specification<AskForleave>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<AskForleave> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(usernames!=null) {
					Predicate name = criteriaBuilder.equal(root.get("studentId"), usernames.getId());
					rules.add(name);
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
	    	
		};
		return spe;
	}
}
